package org.csu.mypetstore.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.UUID;

public class UniqId {
    public static String getOrderNum() {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        Random ran = new Random();
        String randomNumeric="";
        for (int i=0;i<3;i++) {
            randomNumeric += Integer.valueOf(ran.nextInt(10)).toString();
        }

//        再利用UUID生成
        int machineId=1;
        int hashCodeV= UUID.randomUUID().toString().hashCode();
        if (hashCodeV<0){
            hashCodeV=-hashCodeV;
        }
        String str= machineId+String.format("%05d",hashCodeV);
        String orderNum = localDate + randomNumeric + str;
//        log.info("订单号:{}", orderNum);
        return orderNum;
    }
}
